package leetcode.utils;

public class ListNode {
  public int val;
  public ListNode next;

  public ListNode() {
    }

  public ListNode(int val) {
      this.val = val;
    }

  public ListNode(int val, ListNode next) {
    this.val = val;
    this.next = next;
  }

  public ListNode(int[] arrays){
      val = arrays[0];
      int length = arrays.length;
      if (length > 1){
        next = new ListNode(arrays[1]);
        ListNode prev = next;
        if (length > 2){
          for (int i = 2; i < length; i++) {
            prev.next = new ListNode(arrays[i]);
            prev = prev.next;
          }
        }
      }
    }

  @Override
  public String toString() {
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("ListNode Val is:");
    ListNode prev = this;
    while (prev != null){
      stringBuffer.append(prev.val+",");
      prev = prev.next;
    }
    return stringBuffer.toString();
  }
}
